package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.List;

public class Reduce {

    List<Integer> arrayList;

    public Reduce(List<Integer> arrayList) {
        this.arrayList = arrayList;
    }

    public int getMaximum() {
        int ans = Integer.MIN_VALUE;
        for (Integer integer : arrayList) {
            ans = Math.max(ans,integer);
        }
        return ans;
    }

    public double getMinimum() {

        int ans = Integer.MAX_VALUE;
        for (Integer integer : arrayList) {
            ans = Math.min(ans,integer);
        }
        return ans;
    }

    public double getAverage() {
        int sum = 0;
        for (Integer integer : arrayList) {
            sum+=integer;
        }
        return 1.0*sum / arrayList.size();
    }

    public double getOrderedMedian() {
        if(arrayList.size()%2==1){
            return arrayList.get(arrayList.size()/2);
        }else{
            return (1.0*arrayList.get(arrayList.size()/2) + arrayList.get( arrayList.size()/2-1) ) / 2.0;
        }
    }

    public int getFirstEven() {
        for (Integer integer : arrayList) {
            if(integer % 2 ==0) return integer;
        }
        return 0;
    }

    public int getIndexOfFirstEven() {
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i) % 2 ==0) return i;
        }
        return -1;
    }

    public boolean isEqual(List<Integer> arrayList) {
        if(arrayList.size() != this.arrayList.size()){
            return false;
        }
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            if(!arrayList.get(i).equals(this.arrayList.get(i))){
                return false;
            }
        }
        return true;
    }

    public Double getMedianInLinkList(SingleLink singleLink) {
        throw new NotImplementedException();
    }

    public int getLastOdd() {
        for (int i = arrayList.size()-1; i >=0 ; i--) {
            if(arrayList.get(i) % 2 == 1) return arrayList.get(i);
        }
        return  -1;
    }

    public int getIndexOfLastOdd() {
        for (int i = arrayList.size()-1; i >=0 ; i--) {
            if(arrayList.get(i) % 2 == 1) return i;
        }
        return  -1;
    }
}
